package com.ft.oa.service.industryService.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ft.oa.domain.Attachment;
import com.ft.oa.domain.customer.Customer;
import com.ft.oa.domain.dto.knowledgeBase.AttachmentDto;
import com.ft.oa.domain.dto.sys.SecurityUser;
import com.ft.oa.domain.industrys.IndustryProgress;
import com.ft.oa.enums.DirectoryTypeEnum;
import com.ft.oa.enums.ManagerEnum;
import com.ft.oa.mapper.attachment.AttachmentMapper;
import com.ft.oa.mapper.customer.CustomerMapper;
import com.ft.oa.mapper.industrysMapper.IndustryProgressMapper;
import com.ft.oa.service.industryService.IIndustryProgressService;
import com.ft.oa.service.knowledgeBase.DirectoryService;
import com.ft.oa.utils.SecurityUtils;
import com.ft.oa.utils.UploadFileUtile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 行业目标客户进度跟踪Service业务层处理
 * 
 * @author ft
 * @date 2021-12-30
 */
@Service
public class IndustryProgressServiceImpl implements IIndustryProgressService {
    @Resource
    private IndustryProgressMapper industryProgressMapper;

    @Resource
    private CustomerMapper customerMapper;

    @Resource
    private DirectoryService directoryService;

    /**
     * 新增客户跟进记录
     */
    @Override
    public int Customerfollowuprecord(IndustryProgress industryProgress) {
        List<AttachmentDto> attachments = industryProgress.getAttachments();
        if(attachments != null && attachments.size() > 0){
            String json = JSONArray.toJSONString(attachments);
            industryProgress.setAchment(json);
        }
        // 获取当前用户名称用作展示售前项目管理
        Long creator = SecurityUtils.getCurrentUser().getId();
        industryProgress.setCreator(String.valueOf(creator));
        // 返回新增行数
        int insert = industryProgressMapper.insert(industryProgress);
        /**
         * 保存附件项目相关的公共方法
         *
         * @param attachments 附件集合
         * @param source      来源  根据 DirectoryTypeEnum
         * @param relationId  业务id
         * @param sourceId    如果一个表单有多个附件，用于标识附件属于表单的哪个附件
         * @param  projectNumber 项目编号 全局唯一
         */
        directoryService.saveProAttachment(attachments,DirectoryTypeEnum.FILING_ITF_INDUSTRY.getCode(),String.valueOf(industryProgress.getId()),"1",industryProgress.getProjectNumber());
        return insert;
    }

    /**
     * 行业记录
     * @param industId
     */
    @Override
    public HashMap<String,Object> record(String industId,String customId) {
        QueryWrapper<IndustryProgress> progressWrapper = new QueryWrapper<>();
        progressWrapper.eq("industry_id",industId);
        if(ManagerEnum.GENERAL.toString().equals(SecurityUtils.getCurrentUser().getManager())){
            progressWrapper.eq("creator",SecurityUtils.getCurrentUser().getId());
        }
        if(!StringUtils.isEmpty(customId)){
            progressWrapper.eq("customer_company_id",customId);
        }
        // 关联记录表进行查询
        List<IndustryProgress> industryProgresses = industryProgressMapper.selectList(progressWrapper);
        industryProgresses.forEach(e ->{
            String achment = e.getAchment();
            e.setAchment("");
            e.setAttachments(JSONObject.parseArray(achment, AttachmentDto.class));
            e.setCustomername(customerMapper.selectById(e.getCustomerCompanyId()).getCustomerName());
        });
        HashMap<String,Object> progressMap = new HashMap();
        // 多条记录
        progressMap.put("progressdata",industryProgresses);
        return progressMap;
    }


}
